public class MyLinkList implements IList{
    public class ListNode{
        int val;
        ListNode next;
        ListNode prev;
        public ListNode(int val){
            this.val = val;
        }
    }

    public ListNode head;
    public ListNode last;

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            last = node;
            return;
        }
        node.next = head;
        head.prev = node;
        head = node;

    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null){
            head = node;
            last = node;
            return;
        }else {
            node.prev = last;
            last.next = node;
            last = node;
        }

    }

    @Override
    public void addIndex(int index, int data) {
        if (index < 0 || index > size()){
            throw new IndexException("");
        }
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == size()){
            addLast(data);
            return;
        }
        ListNode node = new ListNode(data);
        ListNode cur = findIndex(index);
//        int count = 0;
//        while(count != index){
//            count++;
//            cur = cur.next;
//        }
//        node.prev = cur;
//        node.next = cur.next;
//        cur.next.prev = node;
//        cur.next = node;
        node.prev = cur.prev;
        node.next = cur;
        cur.prev.next = node;
        cur.prev = node;
    }

    private ListNode findIndex(int index){
        ListNode cur = head;
        while(index != 0){
            cur = cur.next;
            index--;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while(cur != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    /*
    * 删除
    * */
    @Override
    public void remove(int key) {
        if (head == null){
            return;
        }
        ListNode cur = head;
//        删除第一个节点存在问题
//        删除最后一个结点存在问题
//        只有一个结点
        while(cur != null){
            if (cur.val == key){
                if (cur == head){
                    head = head.next;
                    if (head != null){
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else {
//                    删除中间
                    if (cur.next != null){
                        cur.next.prev = cur.prev;
                        cur.prev.next = cur.next;
                    }else {
//                    删除尾巴
                        last = last.prev;
                        last.next = null;
                    }
                }
                return;
            }else {
                cur = cur.next;
            }
        }
    }

    @Override
    public void removeAllKey(int key) {
        if (head == null){
            return;
        }
        ListNode cur = head;
//        删除第一个节点存在问题
//        删除最后一个结点存在问题
//        只有一个结点
        while(cur != null){
            if (cur.val == key){
                if (cur == head){
                    head = head.next;
                    if (head != null){
                        head.prev = null;
                    }else {
                        last = null;
                    }
                }else {
//                    删除中间
                    if (cur.next != null){
                        cur.next.prev = cur.prev;
                        cur.prev.next = cur.next;
                    }else {
//                    删除尾巴
                        last = last.prev;
                        last.next = null;
                    }
                }
            }else {
                cur = cur.next;
            }
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        ListNode cur = head;
        while(cur != null){
            ListNode curNext = cur.next;
            cur.next = null;
            cur.next = null;
            cur = curNext;
        }
        this.head = null;
        this.last = null;
    }

    @Override
    public void display() {
        ListNode cur = head;
        while(cur != null){
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
    }


}
